package spring.boot.admin.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import spring.boot.admin.mapper.TCityMapper;
import spring.boot.admin.model.Pages;
import spring.boot.admin.model.TCity;

import java.util.List;

@Slf4j
@Service
@DS("one")
public class TCityService extends ServiceImpl<TCityMapper, TCity> {

    public IPage pageList(Integer page, Integer rows, String pcode) {
        //为适应 easyui的分页插件,稍微修改原参数名称，自定义Pages
        Pages<TCity> pages = new Pages<>(page, rows);
        if(pcode==null||pcode.trim().length()<=0||"0".equalsIgnoreCase(pcode.trim())){
            pcode=null;
        }
        return baseMapper.pageList(pages, pcode);
    }

    public List<TCity> selectList(String provinceCode) {
        QueryWrapper<TCity> wrapper = new QueryWrapper<>();
        wrapper.select("code", "name");
        wrapper.eq("pcode", provinceCode);
        wrapper.orderByAsc("code");
        List<TCity> list = list(wrapper);
        TCity city = new TCity();
        city.setCode("0");
        city.setName("未选择");
        list.add(0,city);
        return list;
    }
}
